﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System;

public class Barrack : Building
{
    public float timer;
    public int Population = 5;
    public Barrack() : base()
    {

    }

    void Start()
    {
        MaxHP = 500;
        price = 15;
        reTrBloodThis = this.transform.GetChild(0);
        reTrBloodThis.GetChild(0).GetComponent<Slider>().maxValue = MaxHP;
        currentHP = MaxHP;
        reTrBloodThis.GetChild(0).GetComponent<Slider>().value = currentHP;
        reTrBloodThis.gameObject.SetActive(false);

        pos = this.transform.position;
        if ((GameJudge.instance.Player1.transform.position.x < 0 && pos.x < 0) || (GameJudge.instance.Player1.transform.position.x > 0 && pos.x > 0)) 
        {
            this.tag = "Player1_Barrack";
            this.name = "Player1_Barrack";
            this.gameObject.layer = 11;
        }
        else
        {
            this.tag = "Player2_Barrack";
            this.name = "Player2_Barrack";
            this.gameObject.layer = 12;
        }
    }

    void Update()
    {
        if (timer >= 1 && GameJudge.instance.Player1.GetComponent<Resource>().coinNum >= new Soldier().price)
        {
            CreateSoldier();
            timer = 0;
        }
        timer += Time.deltaTime;
        NewBlood();
    }

    private void NewBlood()
    {
        reTrBloodThis.GetChild(0).GetComponent<Slider>().value = currentHP;
        if (currentHP < MaxHP)
        {
            reTrBloodThis.gameObject.SetActive(true);
        }
    }

    public void CreateSoldier()
    {
        if (this.transform.position.x > 0)
        {
            pos.x = this.transform.position.x - 3;
        }
        else
        {
            pos.x = this.transform.position.x + 3;
        }
        if (GameJudge.instance.Player1.GetComponent<Camp>().chargeLocal)
        {
            if (this.tag == "Player1_Barrack" && (GameJudge.instance.Player1.GetComponent<Resource>().soldierNum + 1 <= GameJudge.instance.Player1.GetComponent<Resource>().population))
            {
                SpawnManager.instance.CmdCreatSoldier(pos);//在所有端创建士兵
                GameJudge.instance.Player1.GetComponent<Resource>().soldierNum++;
            }
        }
    }
}
